/** 标准设备属性模板库 */

export interface PropertyTemplate {
  /** 标签 */
  label: string
  /** 值/变量名（英文标识） */
  value: string
  /** 单位 */
  unit: string
  /** 数据类型 */
  type: 'double' | 'int' | 'long' | 'string'
  /** 分类 */
  category: '电力' | '水务' | '燃气' | '环境' | '状态' | '通用'
  /** 小数位 */
  decimal?: number
  /** 是否上报 */
  reported?: boolean
  /** 描述 */
  description?: string
}

/** 标准属性模板 */
export const STANDARD_PROPERTIES: PropertyTemplate[] = [
  // ========== 电力相关 ==========
  {
    label: '总电能',
    value: 'dev_consumption',
    unit: 'kWh',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '累计总电能消耗',
  },
  {
    label: '剩余金额',
    value: 'dev_remain_amt',
    unit: '元',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '预付费剩余金额',
  },
  {
    label: '剩余电量',
    value: 'dev_remain_elec',
    unit: 'kWh',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '预付费剩余电量',
  },
  {
    label: '欠费金额',
    value: 'dev_owe_amt',
    unit: '元',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '欠费金额',
  },
  {
    label: '欠费电量',
    value: 'dev_owe_elec',
    unit: 'kWh',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '欠费电量',
  },
  {
    label: 'A相电压',
    value: 'dev_voltage_a',
    unit: 'V',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: 'A相电压',
  },
  {
    label: 'B相电压',
    value: 'dev_voltage_b',
    unit: 'V',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: 'B相电压',
  },
  {
    label: 'C相电压',
    value: 'dev_voltage_c',
    unit: 'V',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: 'C相电压',
  },
  {
    label: 'A相电流',
    value: 'dev_current_a',
    unit: 'A',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: 'A相电流',
  },
  {
    label: 'B相电流',
    value: 'dev_current_b',
    unit: 'A',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: 'B相电流',
  },
  {
    label: 'C相电流',
    value: 'dev_current_c',
    unit: 'A',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: 'C相电流',
  },
  {
    label: '有功功率',
    value: 'dev_active_power',
    unit: 'kW',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: '总有功功率',
  },
  {
    label: '无功功率',
    value: 'dev_reactive_power',
    unit: 'kVar',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: '总无功功率',
  },
  {
    label: '功率因数',
    value: 'dev_power_factor',
    unit: '',
    type: 'double',
    category: '电力',
    decimal: 3,
    reported: true,
    description: '功率因数',
  },
  {
    label: '频率',
    value: 'dev_frequency',
    unit: 'Hz',
    type: 'double',
    category: '电力',
    decimal: 2,
    reported: true,
    description: '电网频率',
  },

  // ========== 水务相关 ==========
  {
    label: '瞬时流量',
    value: 'dev_instant_flow',
    unit: 'm³/h',
    type: 'double',
    category: '水务',
    decimal: 3,
    reported: true,
    description: '瞬时流量',
  },
  {
    label: '总流量',
    value: 'dev_flow',
    unit: 'm³',
    type: 'double',
    category: '水务',
    decimal: 3,
    reported: true,
    description: '累计总流量',
  },
  {
    label: '正向流量',
    value: 'dev_flow_forward',
    unit: 'm³',
    type: 'double',
    category: '水务',
    decimal: 3,
    reported: true,
    description: '正向累计流量',
  },
  {
    label: '反向流量',
    value: 'dev_flow_reverse',
    unit: 'm³',
    type: 'double',
    category: '水务',
    decimal: 3,
    reported: true,
    description: '反向累计流量',
  },
  {
    label: '水压',
    value: 'dev_pressure',
    unit: 'MPa',
    type: 'double',
    category: '水务',
    decimal: 3,
    reported: true,
    description: '水压',
  },
  {
    label: '水温',
    value: 'dev_water_temp',
    unit: '℃',
    type: 'double',
    category: '水务',
    decimal: 2,
    reported: true,
    description: '水温',
  },

  // ========== 燃气相关 ==========
  {
    label: '燃气流量',
    value: 'dev_gas_flow',
    unit: 'm³',
    type: 'double',
    category: '燃气',
    decimal: 3,
    reported: true,
    description: '累计燃气流量',
  },
  {
    label: '燃气压力',
    value: 'dev_gas_pressure',
    unit: 'kPa',
    type: 'double',
    category: '燃气',
    decimal: 2,
    reported: true,
    description: '燃气压力',
  },
  {
    label: '燃气温度',
    value: 'dev_gas_temp',
    unit: '℃',
    type: 'double',
    category: '燃气',
    decimal: 2,
    reported: true,
    description: '燃气温度',
  },

  // ========== 环境监测 ==========
  {
    label: '温度值',
    value: 'dev_temp',
    unit: '℃',
    type: 'double',
    category: '环境',
    decimal: 2,
    reported: true,
    description: '环境温度',
  },
  {
    label: '湿度值',
    value: 'dev_rh',
    unit: '%',
    type: 'double',
    category: '环境',
    decimal: 2,
    reported: true,
    description: '相对湿度',
  },
  {
    label: 'PM2.5',
    value: 'dev_pm25',
    unit: 'μg/m³',
    type: 'double',
    category: '环境',
    decimal: 1,
    reported: true,
    description: 'PM2.5颗粒物浓度',
  },
  {
    label: 'PM10',
    value: 'dev_pm10',
    unit: 'μg/m³',
    type: 'double',
    category: '环境',
    decimal: 1,
    reported: true,
    description: 'PM10颗粒物浓度',
  },
  {
    label: 'CO2',
    value: 'dev_co2',
    unit: 'ppm',
    type: 'double',
    category: '环境',
    decimal: 0,
    reported: true,
    description: '二氧化碳浓度',
  },
  {
    label: 'CH2O',
    value: 'dev_ch2o',
    unit: 'mg/m³',
    type: 'double',
    category: '环境',
    decimal: 3,
    reported: true,
    description: '甲醛浓度',
  },
  {
    label: 'VOC',
    value: 'dev_voc',
    unit: 'mg/m³',
    type: 'double',
    category: '环境',
    decimal: 3,
    reported: true,
    description: '挥发性有机物',
  },
  {
    label: 'O2',
    value: 'dev_o2',
    unit: '%',
    type: 'double',
    category: '环境',
    decimal: 2,
    reported: true,
    description: '氧气浓度',
  },
  {
    label: 'CO',
    value: 'dev_co',
    unit: 'mg/m³',
    type: 'double',
    category: '环境',
    decimal: 3,
    reported: true,
    description: '一氧化碳浓度',
  },
  {
    label: '噪音',
    value: 'dev_noise',
    unit: 'dB',
    type: 'double',
    category: '环境',
    decimal: 1,
    reported: true,
    description: '噪音分贝',
  },
  {
    label: '光照',
    value: 'dev_light',
    unit: 'lux',
    type: 'double',
    category: '环境',
    decimal: 0,
    reported: true,
    description: '光照强度',
  },

  // ========== 状态类 ==========
  {
    label: '上下行',
    value: 'dev_updown',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '上下行状态 (0:下行, 1:上行)',
  },
  {
    label: '运行状态',
    value: 'dev_status',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '设备运行状态 (0:停止, 1:运行)',
  },
  {
    label: '检修状态',
    value: 'dev_fix',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '检修状态 (0:正常, 1:检修中)',
  },
  {
    label: '故障状态',
    value: 'dev_err',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '故障状态 (0:正常, 1:故障)',
  },
  {
    label: '停泊状态',
    value: 'dev_stp',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '停泊状态 (0:未停泊, 1:停泊)',
  },
  {
    label: '在线状态',
    value: 'dev_online',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '在线状态 (0:离线, 1:在线)',
  },
  {
    label: '阀门状态',
    value: 'dev_valve',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '阀门状态 (0:关闭, 1:开启)',
  },
  {
    label: '报警状态',
    value: 'dev_alarm',
    unit: '',
    type: 'int',
    category: '状态',
    reported: true,
    description: '报警状态 (0:正常, 1:报警)',
  },

  // ========== 通用 ==========
  {
    label: '信号强度',
    value: 'dev_signal',
    unit: 'dBm',
    type: 'int',
    category: '通用',
    reported: true,
    description: '通信信号强度',
  },
  {
    label: '电池电量',
    value: 'dev_battery',
    unit: '%',
    type: 'int',
    category: '通用',
    reported: true,
    description: '电池电量百分比',
  },
  {
    label: '电池电压',
    value: 'dev_battery_voltage',
    unit: 'V',
    type: 'double',
    category: '通用',
    decimal: 2,
    reported: true,
    description: '电池电压',
  },
  {
    label: '固件版本',
    value: 'dev_firmware',
    unit: '',
    type: 'string',
    category: '通用',
    reported: false,
    description: '设备固件版本',
  },
  {
    label: '硬件版本',
    value: 'dev_hardware',
    unit: '',
    type: 'string',
    category: '通用',
    reported: false,
    description: '设备硬件版本',
  },
  {
    label: '设备时间',
    value: 'dev_time',
    unit: '',
    type: 'long',
    category: '通用',
    reported: false,
    description: '设备本地时间戳',
  },
]

/** 按分类分组的属性 */
export const PROPERTIES_BY_CATEGORY = STANDARD_PROPERTIES.reduce((acc, prop) => {
  if (!acc[prop.category]) {
    acc[prop.category] = []
  }
  acc[prop.category].push(prop)
  return acc
}, {} as Record<string, PropertyTemplate[]>)

/** 属性分类列表 */
export const PROPERTY_CATEGORIES = ['电力', '水务', '燃气', '环境', '状态', '通用'] as const

/** 根据 value 查找属性模板 */
export function findPropertyTemplate(value: string): PropertyTemplate | undefined {
  return STANDARD_PROPERTIES.find(p => p.value === value)
}

/** 将属性模板转换为设备属性 */
export function templateToProperty(template: PropertyTemplate): Api.DeviceType.Property {
  return {
    name: template.value,
    description: template.label,
    type: template.type,
    unit: template.unit,
    decimal: template.decimal || 0,
    reported: template.reported ?? true,
    isAlarm: false,
    threshold: 0,
    used: 0,
    autoCalc: false,
    scale: 1,
  }
}

